﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Text;
using AsyncHttp.Server;

namespace AsyncHttp
{
    class Program
    {
        static void Main()
        {
            var subject = new Subject<string>();

            using(var server = new HttpServer("http://*/"))
            {
                var listeners = server
                        .Where(ctx => ctx.Request.HttpMethod == "GET")
                        .Subscribe(ctx => subject.Take(1)
                                                 .Subscribe(m => ctx.Respond(new StringResponse(m))));

                var publisher = server
                    .Where(ctx => ctx.Request.HttpMethod == "POST")
                    .Subscribe(ctx => ctx.Request.InputStream.ReadBytes(ctx.Request.ContentLength)
                                          .Subscribe(bts =>
                                                         {
                                                             subject.OnNext(Encoding.UTF8.GetString(bts));
                                                             ctx.Respond(new EmptyResponse(201));
                                                         }));


                Console.ReadLine();

                listeners.Dispose();
                publisher.Dispose();

            }
        }
    }
}
